package com.yuwubao.uav.tool.controller;

import com.yuwubao.framework.bean.JsonResult;
import com.yuwubao.framework.bean.PageData;
import com.yuwubao.framework.loginFilter.LoginRequired;
import com.yuwubao.uav.tool.service.DecisionService;
import com.yuwubao.uav.tool.util.ArticleSearchService;
import com.yuwubao.uav.tool.util.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Min;

/**
 * 决策管理
 */
@RestController
@RequestMapping("/bk")
public class DecisionController {

    private static final Logger logger = LogManager.getLogger(DecisionController.class);
    
    @Resource
    private DecisionService decisionService;

    @Resource
    private ArticleSearchService articleSearchService;

    @LoginRequired
    @PostMapping("/addDecision")
    @ApiOperation(value = "新增决策调研")
    public JsonResult addDecision(
            @ApiParam(value = "决策调研标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "决策调研副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "是否置顶 0 ： 否 1：是") @RequestParam(value = "isTop",required = false) String isTop,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "附件") @RequestParam(value = "attachUrl",required = false) String attachUrl,
            @ApiParam(value = "附件名称") @RequestParam(value = "attachName",required = false) String attachName,
            @ApiParam(value = "图片URL") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "简介") @RequestParam(value = "description",required = false) String description,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("isTop", isTop);
            pageData.put("attachUrl", attachUrl);
            pageData.put("imgUrl", imgUrl);
            pageData.put("img", imgUrl);
            pageData.put("attachName", attachName);
            pageData.put("description", description);

            decisionService.addDecision(pageData);

            try {
                pageData.put("tableName","t_decision");
                articleSearchService.createDoc(pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }



            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateDecision")
    @ApiOperation(value = "更新决策调研")
    public JsonResult updateDecision(
            @ApiParam(value = "决策调研ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "决策调研标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "决策调研副标题") @RequestParam(value = "subTitle",required = false) String subTitle,
            @ApiParam(value = "是否置顶 0 ： 否 1：是") @RequestParam(value = "isTop",required = false) String isTop,
            @ApiParam(value = "内容") @RequestParam(value = "content",required = false) String content,
            @ApiParam(value = "分类ID") @RequestParam(value = "typeId",required = false) String typeId,
            @ApiParam(value = "来源") @RequestParam(value = "origin",required = false) String origin,
            @ApiParam(value = "作者") @RequestParam(value = "author",required = false) String author,
            @ApiParam(value = "附件url") @RequestParam(value = "attachUrl",required = false) String attachUrl,
            @ApiParam(value = "附件名称") @RequestParam(value = "attachName",required = false) String attachName,
            @ApiParam(value = "图片URL") @RequestParam(value = "imgUrl",required = false) String imgUrl,
            @ApiParam(value = "简介") @RequestParam(value = "description",required = false) String description,
            @ApiParam(value = "发布时间") @RequestParam(value = "publicAt",required = false) String publicAt
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("imgUrl", imgUrl);
            pageData.put("img", imgUrl);
            pageData.put("content", content);
            pageData.put("typeId", typeId);
            pageData.put("origin", origin);
            pageData.put("author", author);
            pageData.put("publicAt", publicAt);
            pageData.put("attachName", attachName);
            pageData.put("subTitle", subTitle);
            pageData.put("title", title);
            pageData.put("isTop", isTop);
            pageData.put("attachUrl", attachUrl);
            pageData.put("description", description);
            decisionService.updateDecision(pageData);

            result.setSuccessful();

            try {
                pageData.put("tableName","t_decision");
                articleSearchService.update(id,pageData);
            } catch (Exception e) {
                e.printStackTrace();
            }



        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteDecision")
    @ApiOperation(value = "删除决策调研")
    public JsonResult deleteDecision(
            @ApiParam(value = "决策调研ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);

            decisionService.deleteDecision(pageData);

            try {
                if (StringUtil.isNotNvlOrBlank(ids)) {
                    pageData.put("tableName","t_decision");
                    for (String id : ids.split(",")) {
                        pageData.put("id",id);
                        articleSearchService.delete(pageData);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/disableOrEnableDecision")
    @ApiOperation(value = "禁用或启用决策调研")
    public JsonResult disableOrEnableDecision(
            @ApiParam(value = "决策调研ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            decisionService.disableOrEnableDecision(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listDecision")
    @ApiOperation(value = "获取决策调研列表")
    public JsonResult listDecision(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(decisionService.listDecision(pageData));
            result.setResultNum(decisionService.listDecisionCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addDecisionType")
    @ApiOperation(value = "新增决策调研类型")
    public JsonResult addDecisionType(
            @ApiParam(value = "决策调研类型,多个用逗号分隔", required = true) @RequestParam("names") String names,
            @ApiParam(value = "决策调研类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("names", names);
            pageData.put("remark", remark);

            decisionService.addDecisionType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/updateDecisionType")
    @ApiOperation(value = "更新决策调研类型")
    public JsonResult updateDecisionType(
            @ApiParam(value = "决策调研类型ID", required = true) @RequestParam("id") String id,
            @ApiParam(value = "决策调研类型名称", required = true) @RequestParam("name") String name,
            @ApiParam(value = "决策调研类型备注") @RequestParam(value = "remark",required = false) String remark
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("id", id);
            pageData.put("name", name);
            pageData.put("remark", remark);

            decisionService.updateDecisionType(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @PostMapping("/deleteDecisionType")
    @ApiOperation(value = "删除决策调研类型")
    public JsonResult deleteDecisionType(
            @ApiParam(value = "决策调研类型ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            decisionService.deleteDecisionType(pageData);
            result.setSuccessful();
        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableDecisionType")
    @ApiOperation(value = "禁用或启用专家")
    public JsonResult disableOrEnableDecisionType(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            decisionService.disableOrEnableDecisionType(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listDecisionType")
    @ApiOperation(value = "获取决策调研类型列表")
    public JsonResult listDecisionType(
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(decisionService.listDecisionType(pageData));
            result.setResultNum(decisionService.listDecisionTypeCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }
    @LoginRequired
    @GetMapping("/listDecisionTypeAll")
    @ApiOperation(value = "获取决策调研类型列表所有")
    public JsonResult listDecisionTypeAll(
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("searchKey", searchKey);

            result.setResult(decisionService.listDecisionTypeAll(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/addDecisionComment")
    @ApiOperation(value = "新增决策调研评论")
    public JsonResult addDecisionComment(
            @ApiParam(value = "决策调研ID", required = true) @RequestParam("decisionId") String decisionId,
            @ApiParam(value = "决策调研标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "评论人ID", required = true) @RequestParam("userId") String userId,
            @ApiParam(value = "评论内容",required = true) @RequestParam(value = "content") String content
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("decisionId", decisionId);
            pageData.put("title", title);
            pageData.put("userId", userId);
            pageData.put("content", content);

            decisionService.addDecisionComment(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @PostMapping("/disableOrEnableDecisionComment")
    @ApiOperation(value = "禁用或启用评论")
    public JsonResult disableOrEnableDecisionComment(
            @ApiParam(value = "ID，多个用逗号分隔", required = true) @RequestParam("ids") String ids,
            @ApiParam(value = "0:禁用  1：启用", required = true) @RequestParam("state") String state
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("ids", ids);
            pageData.put("state", state);

            decisionService.disableOrEnableDecisionComment(pageData);
            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listDecisionComment")
    @ApiOperation(value = "获取决策调研评论列表-前端显示使用")
    public JsonResult listDecisionComment(
            @ApiParam(value = "决策ID") @RequestParam(value = "decisionId",required = false) String decisionId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("decisionId", decisionId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(decisionService.listDecisionComment(pageData));
            result.setResultNum(decisionService.listDecisionCommentCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listDecisionCommentForBackend")
    @ApiOperation(value = "获取决策调研评论列表-后台管理使用")
    public JsonResult listDecisionCommentForBackend(
            @ApiParam(value = "决策ID") @RequestParam(value = "decisionId",required = false) String decisionId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("decisionId", decisionId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(decisionService.listDecisionCommentForBackend(pageData));
            result.setResultNum(decisionService.listDecisionCommentCountForBackend(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


    @LoginRequired
    @PostMapping("/addDecisionQuestion")
    @ApiOperation(value = "新增决策调研问卷")
    public JsonResult addDecisionQuestion(
            @ApiParam(value = "决策调研ID", required = true) @RequestParam("decisionId") String decisionId,
            @ApiParam(value = "决策调研标题", required = true) @RequestParam("title") String title,
            @ApiParam(value = "评论人ID", required = true) @RequestParam("userId") String userId,
            @ApiParam(value = "问卷附件名称",required = true) @RequestParam(value = "attachName") String attachName,
            @ApiParam(value = "问卷附件url",required = true) @RequestParam(value = "attachUrl") String attachUrl
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("decisionId", decisionId);
            pageData.put("title", title);
            pageData.put("userId", userId);
            pageData.put("attachName", attachName);
            pageData.put("attachUrl", attachUrl);

            decisionService.addDecisionQuestion(pageData);

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }

    @LoginRequired
    @GetMapping("/listDecisionQuestion")
    @ApiOperation(value = "获取决策调研问卷列表")
    public JsonResult listDecisionQuestion(
            @ApiParam(value = "决策ID") @RequestParam(value = "decisionId",required = false) String decisionId,
            @ApiParam(value = "当前页", required = true) @Min(1) @Length(max = 8) @NotEmpty @RequestParam("page") String page,
            @ApiParam(value = "当前页数量", required = true) @Min(10) @Length(max = 3) @NotEmpty @RequestParam("pageSize") String pageSize,
            @ApiParam(value = "搜索关键字") @RequestParam(value = "searchKey",required = false) String searchKey
    ) {
        JsonResult result = new JsonResult();
        try {
            PageData pageData = new PageData();
            pageData.put("decisionId", decisionId);
            pageData.put("searchKey", searchKey);
            pageData.put("begin", (Integer.valueOf(page) - 1) * Integer.valueOf(pageSize));
            pageData.put("pageSize", Integer.valueOf(pageSize));

            result.setResult(decisionService.listDecisionQuestion(pageData));
            result.setResultNum(decisionService.listDecisionQuestionCount(pageData));

            result.setSuccessful();

        } catch (Exception e) {
            logger.error("", e);
            result.setFailure(e.getMessage());
        }

        return result;
    }


}
